// pages/common/InputBar/InputBar.js
var Mcaptcha = require('../../common/utils/Mcaptcha.js');
var ConstPicker = require('../../common/const/ConstPicker.js');

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    bindData: {
      type: Object,
      value: {
        title: "T",
        holder: "",
        key: "",
        value: "",
        barType: "",
        isRequired: true,
        isEnable: true
      },
      observer: function (newVal, oldVal) { }
    },
    index: {
      type: String,
      value: "0",
      observer: function (newVal, oldVal) { }
    },
    bindExtendType: {
      type: String,
      value: "",
      observer: function (newVal, oldVal) { }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    barInputType: "",
    showComponent: 0,

    pickerData:{},
    pickerIndex: 0,
    pickerSelectKey: "",

    extendButtonTitle: "",
    showExtendComponent: 0
  },

  ready: function () { 
    var showComponent = -1;
    var barInputType = "";
    var pickerData = [];
    var pickerSelectKey = "";

    if (this.data.bindData.barType) {
      if (this.data.bindData.barType.indexOf("KeyBoardType:") === 0) {
        showComponent = 1;
        barInputType = this.data.bindData.barType.replace("KeyBoardType:", "");
      } else if (this.data.bindData.barType.indexOf("HZAreaPickerView:file:") === 0){
        showComponent = 2;
        barInputType = this.data.bindData.barType.replace("HZAreaPickerView:file:", "");  
        pickerData = ConstPicker.constPicker[barInputType];
        pickerSelectKey = pickerData.keys[this.data.pickerIndex];
        console.log("pickerData == " + JSON.stringify(pickerData));
      }

      console.log(showComponent + " -- " + barInputType);
      this.setData({
        showComponent: showComponent,
        barInputType: barInputType,
        pickerData: pickerData,
        pickerSelectKey: pickerSelectKey
      });
    }

    if (this.data.bindExtendType) {
      var extendButtonTitle = "";
      var showExtendComponent = 0;
      if (this.data.bindExtendType.indexOf("captha:") === 0) {
        var mcaptcha = new Mcaptcha({
          el: 'canvas',
          width: 120,
          height: 40,
          code: ""
        });
        showExtendComponent = 1;
        this.setData({
          showExtendComponent: showExtendComponent,
          mcaptcha: mcaptcha,
          captchaCode: "1111"
        });
      } else if (this.data.bindExtendType.indexOf("button:") === 0){
        extendButtonTitle = this.data.bindExtendType.replace("button:", ""); 
        showExtendComponent = 2;
        this.setData({
          extendButtonTitle: extendButtonTitle,
          showExtendComponent: showExtendComponent
        });   
      }
      console.log(extendButtonTitle + " ---- " + showExtendComponent);
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    inputValueChange: function (e) {
      //console.log(JSON.stringify(e));
      var myEventDetail = {
        val: e.detail.value,
        key: this.data.bindData.key
      }
      this.triggerEvent('inputBarValue', myEventDetail) //myevent自定义名称事件，父组件中使用
    },

    pickerValueChange: function (e) {
      //console.log(JSON.stringify(e));
      var pickerSelectKey = this.data.pickerData.keys[e.detail.value];
      //console.log("pickerSelectKey == " + pickerSelectKey);
      this.setData({
        pickerSelectKey: pickerSelectKey
      });

      var pickerSelectValue = this.data.pickerData.values[e.detail.value];     
      var myEventDetail = {
        val: pickerSelectValue,
        key: this.data.bindData.key
      }
      this.triggerEvent('inputBarValue', myEventDetail) //myevent自定义名称事件，父组件中使用
    },

    extandEvent: function (e) {
      var myEventDetail = {
        key: this.data.bindData.key
      }
      if (this.data.bindExtendType) {
        if (this.data.bindExtendType.indexOf("captha:") === 0) {
          var captchaCode = this.data.mcaptcha.genRandomCode();

          console.log(this.data.mcaptcha);
          console.log(captchaCode);
          
          this.data.mcaptcha.refresh(captchaCode);
          myEventDetail.val = captchaCode;
        }
      }

      this.triggerEvent('extandEvent', myEventDetail) //myevent自定义名称事件，父组件中使用
    }
  }

});

